import router from "../router";
import bus from "../utils/bus/index.js";
// import * as api from "../api/menu.js";
import { usePermission } from "../store/permission.js";

router.beforeEach(async (to, from) => {
  let permissionStore = usePermission();

  // permissionStore.getMeunList();

  // console.log(res,"getMenuList");
  if (!sessionStorage.getItem("token") ) {
    if(to.path !== "/login"){
      return {
        path: "/login",
      };
    }
  } else {
    if (permissionStore.meunList.length === 0) {
      //要等一下他  await permissionStore.getMeunList()
      // console.log("lllll");
      await permissionStore.getMeunList();
      // console.log("lllll");
      return {
        ...to
      };
    }
  }
});

router.afterEach((to, from) => {
  console.log("afterEach=====", to);
  console.log("afterEach=====", from);
  bus.emit("list", to.matched);
});
